﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Petzold.Media3D;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using System.ComponentModel;

namespace PistonWheel
{
    public class Piston:Cylinder,INotifyPropertyChanged,ICloneable
    {
        private double _time;
        private Wheel Wheel { get; set; }
        private Rod Rod { get; set; }

        public double Mass { get; set; }
        public double Height { get; set; }

        public double Time
        {
            get
            {
                return this._time;
            }
            set
            {
                if (this._time != value)
                {
                    this._time = value;
                    this.NotifyPropertyChanged("Time");
                }
            }
        }

        public double YPos
        {
            get
            {
                return (this.Point1.Y + this.Point2.Y) / 2.0;
            }
        }

        public double Speed
        {
            get
            {
                return -this.Wheel.Radius * Math.Sin( this.Wheel.Angle ) - (Math.Pow( this.Wheel.Radius, 2 ) *
                    Math.Cos( this.Wheel.Angle ) * Math.Sin( this.Wheel.Angle )) /
                    (Math.Sqrt( Math.Pow( this.Rod.L, 2 ) - Math.Pow( this.Wheel.Radius * Math.Sin( this.Wheel.Angle ), 2 ) ));
            }
        }

        public Piston(Wheel wheel, Rod rod, Brush backBrushMaterial, Brush frontBrushMaterial)
            : base()
        {
            this.Wheel = wheel;
            this.Rod = rod;
            DiffuseMaterial backMaterial = new DiffuseMaterial( backBrushMaterial );
            this.BackMaterial = backMaterial;
            DiffuseMaterial frontMaterial = new DiffuseMaterial( frontBrushMaterial );
            this.Material = frontMaterial;
            this.Mass = 1.0;
            this.Height = 2.0;
        }

        #region INotifyPropertyChanged Members

        public new event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged( this, new PropertyChangedEventArgs( info ) );
            }
        }
        #endregion

        #region ICloneable Members

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion
    }
}
